var helloApp = angular.module('helloApp', ['ui.router','ngAnimate','flow','ngCookies','pascalprecht.translate',
    'ngDragDrop','ngTable']);

helloApp.config(function($stateProvider, $urlRouterProvider, $locationProvider, $translateProvider, $windowProvider) {

    $urlRouterProvider.otherwise("/login");

    $translateProvider.useStaticFilesLoader({
        prefix: 'translation/',
        suffix: '.json'
    });
});

helloApp.config(['flowFactoryProvider', function (flowFactoryProvider) {
    flowFactoryProvider.defaults = {
        target: '/api/user/uploadImage',
        permanentErrors:[404, 500, 501],
        minFileSize: 0,
        singleFile: true
    };

    flowFactoryProvider.on('catchAll', function (event) {
        console.log(event);
    });
}]);

helloApp.run(function ($rootScope, $state, Auth, $window, LangService) {

    LangService.changeLang($window.navigator.language);

    $rootScope.$on("$stateChangeStart", function (event, toState, toParams, fromState, fromParams) {
        if (!Auth.authorize(toState.data.access)) {
            $rootScope.error = "Seems like you tried accessing a route you don't have access to...";
            event.preventDefault();

            if(fromState.url === '^') {
                if(Auth.isLoggedIn()) {
                    $state.go('root.admin.home');
                } else {
                    $rootScope.error = null;
                    $state.go('root.anon.login');
                }
            }
        }
    });
});